<?php 
namespace Gigasweb\Wordpress\Metabox;

class Paragraph {
	private $_Content;
	private $_Name;
	private $_Nonce;
	
	public function __construct( $name, $content = array() ){
		$this->_Content = $content;
		$this->_Name = $name;
		$this->_Nonce = rand();
	}	
	
	private function render_paragraph_box( $id, $title, $content ) {
		?><div class="wrapper-paragraph" style="margin-top: 20px;"><input class="title" name="<?php echo $this->_Name; ?>[<?php echo $id; ?>][title]" placeholder="<?php echo __( "Titolo" ); ?>" value="<?php echo $title; ?>" /> <a href="javascript:;" class="deleteParagraph"><?php echo __( "Delete" ); ?></a><?php
			wp_editor( $content, $id, array( 'textarea_name' => $this->_Name . '[' . $id . '][content]' ) );
		?></div><?php
	}
	
	public function Render() {
		?>
		<div class="">
			<div>
	        	<a href="javascript:addParagraph<?php echo $this->_Nonce; ?>();" class="button"><?php echo __( "Aggiungi" ); ?></a>
	            <div id="wrapper-paragraphs-<?php echo $this->_Nonce; ?>" class="wrapper-paragraphs" style="margin-top: 20px;">
		            <?php 
						$init_script = "";
						if( is_array( $this->_Content ) ){
							foreach( $this->_Content as $key=>$p ) {
								$this->render_paragraph_box( $key, $p['title'], $p['content'] );
								//$init_script .= "init_editor('$key'); ";
							}
						}
					?>
		             <script type="text/javascript">
					//<![CDATA[
						jQuery(document).ready(function(){
							<?php echo $init_script; ?>					
						});
					//]]>
					</script>
            	</div>
        	</div>
	        <script type="text/javascript">
			//<![CDATA[			
				<?php
				ob_start();
				$this->render_paragraph_box( 'new_editor_id', '', '' );
				$template = ob_get_clean();
				?>
				jQuery(document).ready(function($){
					$('#wrapper-paragraphs-<?php echo $this->_Nonce; ?> .deleteParagraph').live('click',function(){
						
						var p=$(this).parent();
						p.slideUp(500,function(){
							jQuery('html,body').animate({scrollTop: p.offset().top-$(window).height()});
							p.remove();
						});
					});
				});
				
				function init_editor(id){
					var mceInit ={mode:"exact",width:"100%",theme:"advanced",skin:"wp_theme",language:"it",spellchecker_languages:"+English=en,Danish=da,Dutch=nl,Finnish=fi,French=fr,German=de,Italian=it,Polish=pl,Portuguese=pt,Spanish=es,Swedish=sv",theme_advanced_toolbar_location:"top",theme_advanced_toolbar_align:"left",theme_advanced_statusbar_location:"bottom",theme_advanced_resizing:true,theme_advanced_resize_horizontal:false,dialog_type:"modal",formats:{
						alignleft : [
										{selector : 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles : {textAlign : 'left'}},
										{selector : 'img,table', classes : 'alignleft'}
									],
									aligncenter : [
										{selector : 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles : {textAlign : 'center'}},
										{selector : 'img,table', classes : 'aligncenter'}
									],
									alignright : [
										{selector : 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles : {textAlign : 'right'}},
										{selector : 'img,table', classes : 'alignright'}
									],
									strikethrough : {inline : 'del'}
								},
						relative_urls:false,
						remove_script_host:false,
						convert_urls:false,
						remove_linebreaks:true,
						gecko_spellcheck:true,
						keep_styles:false,
						entities:"38,amp,60,lt,62,gt",
						accessibility_focus:true,
						tabfocus_elements:"major-publishing-actions",
						media_strict:false,
						paste_remove_styles:true,
						paste_remove_spans:true,
						paste_strip_class_attributes:"all",
						paste_text_use_dialog:true,
						extended_valid_elements:"article[*],aside[*],audio[*],canvas[*],command[*],datalist[*],details[*],embed[*],figcaption[*],figure[*],footer[*],header[*],hgroup[*],keygen[*],mark[*],meter[*],nav[*],output[*],progress[*],section[*],source[*],summary,time[*],video[*],wbr",
						wpeditimage_disable_captions:false,
						wp_fullscreen_content_css:"<?php bloginfo( 'url' ); ?>/wp-includes/js/tinymce/plugins/wpfullscreen/css/wp-fullscreen.css",
						plugins:"inlinepopups,spellchecker,tabfocus,paste,media,fullscreen,wordpress,wpeditimage,wpgallery,wplink,wpdialogs,wpfullscreen",
						elements:id,
						wpautop:true,
						apply_source_formatting:false,
						theme_advanced_buttons1:"bold,italic,strikethrough,|,bullist,numlist,blockquote,|,justifyleft,justifycenter,justifyright,|,link,unlink,wp_more,|,spellchecker,wp_fullscreen,wp_adv",
						theme_advanced_buttons2:"formatselect,underline,justifyfull,forecolor,|,pastetext,pasteword,removeformat,|,charmap,|,outdent,indent,|,undo,redo,wp_help",
						theme_advanced_buttons3:"",
						theme_advanced_buttons4:""}; 
					var qtInit = {id:id,buttons:"strong,em,link,block,del,ins,img,ul,ol,li,code,more,spell,close,fullscreen"};
					
					if ( typeof( tinyMCE ) == "object" && typeof( tinyMCE.execCommand ) == "function" ) {
						var init, ed, qt, first_init, mce = true;
						// mark wp_theme/ui.css as loaded
						tinymce.DOM.files[tinymce.baseURI.getURI() + '/themes/advanced/skins/wp_theme/ui.css'] = true;
	
						if ( mce )
							try { tinymce.init(mceInit); } catch(e){
								alert(e);
							}
						
						if ( typeof(QTags) == 'function' ) {
							try { quicktags( qtInit ); } catch(e){
								//alert(e);
							}
						}
					}
				}
				
				function addParagraph<?php echo $this->_Nonce; ?>() {
					var s='new_editor_id';
					var template=<?php echo json_encode( $template ); ?>;
					var id='paragraph-'+ (new Date()).getTime();
					template=template.replace(/new_editor_id/g,id);
					<?php /*jQuery('#wrapper-paragraphs').append('<div class="wrapper-paragraph"><input class="title" name="paragraph-title[]" placeholder="<?php echo __( "Titolo" ); ?>" /><textarea id="'+id+'" name="paragraph-content[]"></textarea></div>');*/ ?>
					var newnode=jQuery(template);
					jQuery('#wrapper-paragraphs-<?php echo $this->_Nonce; ?>').append(newnode);
					<?php /*jQuery("#"+id).addClass("mceEditor");*/ ?>
					
					init_editor(id);
					
					jQuery('html,body').animate({scrollTop: newnode.offset().top-100});
				}
				
				function set_urls(editor_init){var t=editor_init,sl=tinymce.ScriptLoader,ln=t.ref.language,th=t.ref.theme,pl=t.ref.plugins;sl.markDone(t.base+'/langs/'+ln+'.js');sl.markDone(t.base+'/themes/'+th+'/langs/'+ln+'.js');sl.markDone(t.base+'/themes/'+th+'/langs/'+ln+'_dlg.js');sl.markDone(t.base+'/themes/advanced/skins/wp_theme/ui.css');tinymce.each(pl.split(','),function(n){if(n&&n.charAt(0)!='-'){sl.markDone(t.base+'/plugins/'+n+'/langs/'+ln+'.js');sl.markDone(t.base+'/plugins/'+n+'/langs/'+ln+'_dlg.js');}});}
			//]]>
			</script>
		</div><?php
	}
}